import { ref } from "vue";
import { SparePartBomListItem } from '../types'
import apiParts from '@/util/network_api/parts';

/**
 * 根据产品ID获取产品Boom子件列表
 */ 
export default function() {
	const bomList = ref<Array<SparePartBomListItem>>([])
	const total = ref(0)
	const getBomListParam = ref({
		limit: 10,
		page: 1,
	})
	
	const getBomList = async (sparePartId: string, isAppend = true) => {
		try {
			const res = await apiParts.getSparePartBomList(sparePartId, getBomListParam.value)
			if (res.code === 20000) {
				total.value = Number(res.data.total)
				bomList.value = res.data.rows
				return ''
			}
			
			return res.message
		} catch(e) {
			return '获取Bom列表失败，请稍后重试'
		}
	}
	
	const resetGetBomListParam = () => {
		getBomListParam.value = {
			page: 1,
			limit: 10,
		}
	}
		
	return {
		total,
		bomList,
		getBomListParam,
		getBomList,
		resetGetBomListParam,
	}
}